<template>
    <div>
        <div style="margin-bottom: 20px;">
            <el-input v-model="query.name" placeholder="请输入会员名称" style="width: 200px;margin-right: 20px;"></el-input>
            <el-button type="primary" @click="select">查询</el-button>
        </div>
        <div v-if="store.state.user.type == 3">
            <el-button type="primary" @click="add(null)">新增会员</el-button>
            <el-button type="danger" @click="handDelete" :disabled="!ids.length">批量删除</el-button>

        </div>
        <el-table border :data="list" style="width: 100%;margin-top: 20px;"  @selection-change="handleSelectionChange">
            <el-table-column
            type="selection"
            width="55">
          </el-table-column>
        
            <el-table-column prop="name" label="会员名称" />
            <el-table-column prop="jifen" label="积分" />
          
<el-table-column label="操作">
    <template #default="scope">
                    <!-- {{scope.row}} -->
                    <el-button type="text" @click="add(scope.row)">修改</el-button>
                    <el-button type="text" @click="handDelete(scope.row)" v-if="store.state.user.type == 3&&scope.row.id!=store.state.user.id">删除</el-button>
                </template>
</el-table-column>
<!-- <el-table-column prop="address" label="Address" /> -->
</el-table>
<el-pagination v-show="total>0" style="margin-top: 20px;" :currentPage="query.page" :page-sizes="[100, 200, 300, 400]" background layout="total,  prev, pager, next, jumper" :page-size="query.size" :total="total" @current-change="currentChange"></el-pagination>
<el-dialog destroy-on-close v-model="dialogVisible" :title="type=='add'?'新增会员':'修改会员'" width="30%" :before-close="handleClose">
    <el-form :model="form" :rules="rules" ref="formRef" :inline="false" size="default">
        <el-form-item prop="name" style="width: 100%;">
            <el-input v-model="form.name" placeholder="请输入会员名称" style="width: 100%;">
            </el-input>
        </el-form-item>
        <el-form-item prop="jifen" style="width: 100%;">
            <el-input v-model="form.jifen" placeholder="请输入积分" style="width: 100%;" type="number" >
            </el-input>
        </el-form-item>
        <el-form-item prop="color" style="width: 100%;">
            <el-color-picker v-model="form.color"></el-color-picker>

        </el-form-item>
    </el-form>
    <template #footer>
  <span class="dialog-footer">
    <el-button @click="dialogVisible = false">取消</el-button>
    <el-button type="primary" @click="submit">
      确定
    </el-button>
  </span>
</template>
</el-dialog>
</div>
</template>
<script setup>
    import {
        onMounted,
        ref,
        reactive
    } from 'vue'
    import {
      gethuiyuan,addhuiyuan,updatehuiyuan,delhuiyuan
    } from "~/api/xm.js"
    import {
        useStore
    } from 'vuex'
    import {
        ElMessage,
        ElMessageBox
    } from 'element-plus'
    import {
        Plus
    } from '@element-plus/icons-vue'
    const store = useStore()
    const ids = ref([])
    const list = ref([])
    const total = ref(0)
    const type = ref('')
    const id = ref('')
    const dialogVisible = ref(false)
    const query = ref({
        page: 1,
        size: 10,
        name: '',
        type: 1,
        id: store.state.user.type == 1 ? store.state.user.id : null
    })
    const form = reactive({
        name: "",
        avatar: ''
    })

    function handleAvatarSuccess(e) {
        form.avatar = e.data
        console.log(e);
    }

    function currentChange(e) {
        query.value.page = e
        select()
        console.log(e);
    }

    function handleSelectionChange(e) {

        ids.value = e.map(v => v.id)
        console.log(ids.value);
    }
    const validatePass = (rule, value, callback) => {
        console.log(value);
        if (value === '') {
            callback(new Error('手机号不能为空'))
        } else {
            let reg = /^1[3-9]\d{9}$/
            if (reg.test(value)) {
                callback()

            }
            callback(new Error('手机号格式不正确'))
        }
    }
    const rules = {
        name: [{
            required: true,
            message: '账号不能为空',
            trigger: 'change',
        }],
        color: [{
            required: true,
            message: '颜色不能为空',
            trigger: 'change',
        }],
        jifen: [{
            required: true,
            message: '不能为空',
            trigger: 'change',
        }],
    }
    onMounted(() => {
        select()
    })

    function handDelete(row) {
        let id = row.id ? row.id : ids.value.join(',')
        console.log(id);
        ElMessageBox.confirm(
                `是否删除id为${id}的数据?`,
                'Warning', {
                    confirmButtonText: '确定删除',
                    cancelButtonText: '取消删除',
                    type: 'warning',
                }
            )
            .then(() => {
                delhuiyuan(id).then(res => {
                    if (res.code == 200) {
                        ElMessage({
                            type: 'success',
                            message: 'Delete completed',
                        })
                        select()
                    }
                })

            })

    }

    function add(e) {
        form.id = ''
        id.value = ''
        form.name = ''
        form.jifen = ''
        form.color = ''
        type.value = "add"
        dialogVisible.value = true
        if (e && e.id) {
            type.value = "edit"
            id.value = e.id
            form.name = e.name
            form.jifen = e.jifen
            form.color = e.color
        }
    }

    function select() {
        gethuiyuan(query.value).then(res => {
            total.value = res.data.total
            list.value = res.data.list
            console.log(res);
        })
    }
    const formRef = ref(null)
    const rowId = ref('')

    function submit() {
        formRef.value.validate((valid) => {
            if (valid) {
                if (type.value == "add") {
                    addhuiyuan(form).then(res => {
                        if (res.code == 200) {
                            ElMessage({
                                type: 'success',
                                message: '新增成功',
                            })
                            dialogVisible.value = false
                            select()
                        }

                    })
                } else {
                    form.id = id.value
                    updatehuiyuan(form).then(res => {
                        if (res.code == 200) {
                            ElMessage({
                                type: 'success',
                                message: '修改成功',
                            })
                            dialogVisible.value = false
                            select()
                        }
                    })
                }


            }
        })
    }

    function deleteRow(row) {
        ElMessageBox.confirm(
                '是否要删除?',
                '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning',
                }
            )
            .then(() => {
                deletee(row.id).then(res => {
                    if (res.code == 200) {
                        ElMessage({
                            type: 'success',
                            message: '删除成功',
                        })
                        select()
                    }
                })

            })

    }
</script>
<style scoped>
    .avatar-uploader .avatar {
        width: 178px;
        height: 178px;
        display: block;
    }
</style>

<style>
    .avatar-uploader .el-upload {
        border: 1px dashed var(--el-border-color);
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
        transition: var(--el-transition-duration-fast);
    }
    
    .avatar-uploader .el-upload:hover {
        border-color: var(--el-color-primary);
    }
    
    .el-icon.avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 178px;
        height: 178px;
        text-align: center;
    }
</style>